package cn.hnkjxy.web.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hnkjxy.po.Product;
import cn.hnkjxy.service.ProductService;

@Controller
public class ProductController {
	@Autowired
	private ProductService productService;

	@RequestMapping(value = "/findAllProduct")
	public String findAllProduct(Model model) {
		List<Product> products = this.productService.findAllProduct();
		model.addAttribute("products", products);
		return "index";
	}

	@RequestMapping(value = "/findProductById", method = RequestMethod.GET)
	public String findProductById(Integer prid, HttpSession session) {
		Product product = this.productService.findProductById(prid);
		session.setAttribute("product", product);
		return "order";
	}

	@RequestMapping(value = "/modifyproductstatus")
	public String modifyproductstatus(HttpSession session, Model model) {
		Product product = (Product) session.getAttribute("product");
		Integer row = this.productService.modifyproductstatus(product.getPrid(), 1);
		if (row == 1) {
			model.addAttribute("msg", "支付成功");
		} else {
			model.addAttribute("msg", "支付失败");
		}
		return "paysuccess";
	}
	
	@RequestMapping(value="/admin/ListProduct")
	public String ListProduct(Model model) {
		List<Product> products = this.productService.findAllProduct();
		List<Double> prices = this.productService.findAllPrice();
		model.addAttribute("prices", prices);
		model.addAttribute("products", products);
		return "admin/product";
	}
	
	@RequestMapping(value="/admin/deleteProduct")
	@ResponseBody
	public String deleteProduct(Integer prid) {
		Integer row = this.productService.deleteProduct(prid);
		if(row > 0) {
			return "ok";
		}else {
			return "no";
		}
	}
	
	@RequestMapping(value = "/admin/findProductById", method = RequestMethod.GET)
	@ResponseBody
	public Product findProductById(Integer prid) {
		Product product = this.productService.findProductById(prid);
		return product;
	}
	
	@RequestMapping(value="/admin/modifyproductstatus2")
	@ResponseBody
	public String modifyproductstatus2(Product product) {
		Integer row = this.productService.modifyproductstatus2(product);
		if(row > 0) {
			return "ok";
		}else {
			return "no";
		}
	}
	
	@RequestMapping(value="/admin/findProductByCondition")
	public String findPuductByCondition(String roomnumber,String type,Double price,Model model) {
		Product product = new Product();
		product.setRoomnumber(roomnumber);
		product.setType(type);
		product.setPrice(price);
		List<Product> products = this.productService.findProductByCondition(product);
		List<Double> prices = this.productService.findAllPrice();
		model.addAttribute("prices", prices);
		model.addAttribute("products", products);
		return "admin/product";
	}
	
	@RequestMapping(value="/admin/addProduct")
	@ResponseBody
	public String addProduct(Product product) {
		Integer row = this.productService.addProduct(product);
		if(row > 0) {
			return "ok";
		}else {
			return "no";
		}
	}
	
	@RequestMapping(value="/admin/modifyproductstatus")
	@ResponseBody
	public String addProduct(Integer prid) {
		Integer row = this.productService.modifyproductstatus(prid,0);
		if(row > 0) {
			return "ok";
		}else {
			return "no";
		}
	}
	
	@RequestMapping("/admin/verificationProduct")
	@ResponseBody
	public Integer verificationProduct(String roomnumber) {
		Integer prid = this.productService.verificationProduct(roomnumber);
		return prid;
	}
}
